package ferp.android.views;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;

/**
* User: igorgok
* Date: 6/14/12 4:51 PM
*/ // element represents a link between a view and it's current area
abstract class Element
{
  public Element(Context context, ViewGroup parent)
  {
    view = setup(context);
    view.setVisibility(View.INVISIBLE);

    parent.addView(view);
  }

  //public View view() {return view;}
  //public Rect area() {return area;}

  public void layout(Rect area)
  {
    this.area = area;

    layout();
  }

  public void layout()
  {
    if (area != null)
    {
      view.measure(View.MeasureSpec.EXACTLY | area.width(), View.MeasureSpec.EXACTLY | area.height());

      // adjust parameters if needed
      onBeforeLayout(area);

      view.layout(area.left, area.top, area.right, area.bottom);
    }
  }

  public void show(Rect area)
  {
    layout(area);
    view.setVisibility(View.VISIBLE);
    onShow();
  }

  public void show(boolean visible)
  {
    // currently we do not fire onHide and onShow events here, so do not use it with animated elements e.g. "sand clock"
    view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
  }

  public void hide()
  {
    onHide();
    view.setVisibility(View.INVISIBLE);
  }

  protected abstract View setup(Context context);
  protected void onBeforeLayout(Rect area) {}
  protected void onShow() {}
  protected void onHide() {}

  protected final View view;
  private Rect area;
}
